package com.cms.controller;

import com.cms.entity.Clazz;
import com.cms.service.ClazzService;
import com.cms.vo.Result;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/clazz")
@Slf4j
public class ClazzController {

    @Resource
    private ClazzService clazzService;


    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Clazz clazz){
        Integer save = clazzService.save(clazz);
        if (save != 1) {
            return new Gson().toJson(Result.error(501, "保存失败"));
        }
        return new Gson().toJson(Result.success());
    }


    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String lists(HttpSession session){
        log.info("获取班级列表 :: {}", clazzService.list());
        session.setAttribute("clazzList", clazzService.list());
        return new Gson().toJson(Result.success(clazzService.list()));
    }

    @PostMapping("/delete")
    @ResponseBody
    public String delete(Integer cid){
        log.info("删除班级 :: {}", cid);
        Integer delete = clazzService.delete(cid);
        if (delete != 1) {
            return new Gson().toJson(Result.error(501, "删除失败"));
        }
        return new Gson().toJson(Result.success());
    }

    @PostMapping(value = "/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(Clazz clazz){
        log.info("更新班级信息 :: {}", clazz);
        Integer update = clazzService.update(clazz);
        if (update != 1) {
            return new Gson().toJson(Result.error(501, "更新失败"));
        }
        return new Gson().toJson(Result.success());
    }



    // 获取saveClazz页面
    @GetMapping("/savePage")
    public String save(){
        return "saveClazz";
    }


    // 获取listClazz页面
    @GetMapping("/listPage")
    public String list() {
        return "listClazz";
    }


    // 获取updateClass页面
    @GetMapping("/updatePage")
    public String update() {
        return "updateClass";
    }


    // 获取clazzScoreSimple页面
    @GetMapping("/clazzScoreSimplePage")
    public String clazzScoreSimple() {
        return "clazzScoreSimple";
    }




}
